<template>
  <!-- 工时维护店端-经销商端页面 -->
  <div>
    <!--条件筛选-->
    <FormPanel ref="formPanel" :list="formList" :cols="3" :labelWidth="110" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <!--操作 -->
    <!--<button-area :style="{ paddingLeft: '80px' }"></button-area>-->
    <!-- 列筛选-->
    <!--<FilterTable-->
    <!--ref="table"-->
    <!--columnsRef="repair-labourHour-info"-->
    <!--:isSelectColumn="false"-->
    <!--:columns="columns"-->
    <!--:dataSource="dataSource"-->
    <!--:datakey="'rows'"-->
    <!--:onColumnsChange="columns => (this.columns = columns)"-->
    <!--:onSyncTableData="syncTableFun"-->
    <!--&gt;-->
    <!--&lt;!&ndash;列操作&ndash;&gt;-->
    <!--<template slot="controls">-->
    <!--<el-button v-show="!(this.type === 'show')" :disabled="addModelBtnDisable" size="small" type="primary" icon="el-icon-plus" @click="addRowClick">添加车型</el-button>-->
    <!--</template>-->
    <!--</FilterTable>-->

    <div :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }">
      <AjaxButton v-show="!(this.type === 'show')" type="primary" :click="saveDialog">保 存</AjaxButton>
      <el-button @click="closeDialog">关 闭</el-button>
      <!--<BaseDialog :visible.sync="showChooseModel" title="车型选择" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">-->
      <!--<chooseModel selectionType="single" @close="closeChooseModelDialog" />-->
      <!--</BaseDialog>-->
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import dictCode from '@/utils/dictCode';
import { getSeries } from '@/api/repair/estimateExtension';
import { getLabour, saveLabour } from '@/api/repair/repairBusiness/labourHour/index.js';
import FormPanel from '../../../components/FormPanel/FormPanel.vue';
import chooseModel from '@/pages/repair/repairBusiness/labourHour_oem/chooseModel';
import { partCodeP } from '@/utils/validate.js';
import { editRepairItem, getRepairDetail, getCarSeries, getCarModels, } from '@/api/claim/dealer/AuthMaintenance'
// 延保订单查询
export default {
  name: 'repair-labourHour-info',
  props: {
    row: {
      type: Object,
      default: {}
    },
    type: {
      // edit show add三种状态
      type: String,
      default: 'add'
    },
    modelList: {
      type: Array,
    },
    seriesList: {
      type: Array,
    }
  },
  components: { FormPanel, chooseModel },
  mixins: [dictionary],
  data() {
    return {
      showChooseModel: false,
      // columns: this.createTableColumns(),
      dataSource: [],
      handleType: '',
      formData: {},
      tableData: [],
      selectFlag: false,
      formList: this.createFormList(),
      estimate: false,
      addModelBtnDisable: false
    };
  },
  mounted() {
    if (this.type === 'add') {
    } else {
      this.formList.forEach(x => {
        x.initialValue = this.row[x.fieldName];
        if (x.fieldName === 'labourCode') {
          x.disabled = true; // 不允许编辑
        }
      });
      this.getLabour();
      if (this.type === 'show') {
        this.formList.forEach(x => {
          x.disabled = true;
          x.rules = [];
        });
      }
    }
  },
  methods: {
     getLabour() {
      getRepairDetail({ itemId: this.row.itemId, dataSources: this.row.dataSources }).then(res=>{

      if (res.resultCode === 200) {

        let data = res.data[0] || [];
        console.log(data)
        this.formList.find(x => x.fieldName == 'labourCode').initialValue = data.labourCode
        this.formList.find(x => x.fieldName == 'labourName').initialValue = data.labourName
        this.formList.find(x => x.fieldName == 'itemStatus').initialValue = data.itemStatus
        // this.formList.find(x => x.fieldName == 'seriesCode').initialValue = data.seriesCode
        this.formList.find(x => x.fieldName == 'modelCode').initialValue = data.modelCode
        this.formList.find(x => x.fieldName == 'stdLabourHour').initialValue = data.stdLabourHour
      }
      });
    },
    getCarSeriesData() {
      let dataArr = [];
      let tempArr = [];
      getCarSeries().then((res) => {
        if (res.resultCode === 200) {
          res.data.forEach((item) => {
            if (item.SERIES_NAME && !tempArr.includes(item.SERIES_NAME)) {
              tempArr.push(item.SERIES_NAME);
              dataArr.push({ text: item.SERIES_NAME, value: item.SERIES_CODE });
            }
          });
        }
      });
      return dataArr;
    },

    getCarModelData() {
      // code
      // console.log(this.seriesList)
      // let series = this.seriesList.find(item => item.value === code);
      getCarModels().then((res) => {
        console.log(res)
        if (res.resultCode === 200) {
          this.formList.find(item => item.fieldName === 'modelCode').initialValue = '';
          this.formList.find(item => item.fieldName === 'modelCode').itemList = res.data.map(x => ({ text: x.MODEL_NAME, value: x.MODEL_CODE1 }));
        }
      });
    },

    createFormList() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '工时信息',
          style: {}
        },
        {
          type: 'INPUT',
          label: '工时代码',
          fieldName: 'labourCode',
          rules: [
            { required: true, message: '请输入...', trigger: 'change' },
            { required: true, validator: partCodeP, trigger: 'change' },
            { max: 16, message: '工时代码不能超过16个字符', trigger: 'change' }
          ]
        },
        {
          type: 'INPUT',
          label: '工时名称',
          fieldName: 'labourName',
          rules: [{ required: true, message: '请输入...', trigger: 'change' }],
          maxlength: 100
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'itemStatus',
          itemList: this.createDictList('1003'),
          rules: [{ required: true, message: '请输入...', trigger: 'change' }]
        },
        // {
        //   type: 'SELECT',
        //   label: '车系',
        //   fieldName: 'seriesCode',
        //   placeholder: '请选择...',
        //   initialValue: '',
        //   itemList: this.seriesList,
        //   rules: [{ required: true, message: '请输入...', trigger: 'change' }],
        //   change: (val) => {
        //     if (val) {
        //       this.getCarModelData(val);
        //     } else {
        //       this.formList.find(item => item.fieldName === 'modelCode').itemList = [];
        //       this.formList.find(item => item.fieldName === 'modelCode').initialValue = '';
        //     }
        //   }
        // },
        {
          type: 'SELECT',
          label: '车型',
          fieldName: 'modelCode',
          placeholder: '请选择...',
          itemList: this.modelList,
          rules: [{ required: true, message: '请输入...', trigger: 'change' }],
        },
        {
          type: 'INPUT_NUMBER',
          label: '索赔工时',
          fieldName: 'stdLabourHour',
          rules: [{ required: true, message: '请输入...', trigger: 'change' }],
          editType: 'number',
          precision: 2,
          max: 99999999
        },
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      let [err, data] = await this.$refs.formPanel.GET_FORM_DATA();
      if (err) return;
      if (this.type != 'add') {
        data = Object.assign({}, data, { itemId: this.row.itemId });
      }
      let json = JSON.parse(JSON.stringify(data))
      let res = await editRepairItem(this.type, json);
      if (res.resultCode === 200) {
        this.$notify({
          title: '提示信息',
          message: '保存成功！',
          type: 'success'
        });
        this.closeDialog('refresh');
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
  }
};
</script>
